﻿package mortal.game.view.guild.otherpanel
{
    import Message.Game.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guild.cellrender.*;
    import mortal.game.view.mail.view.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildMailPanel extends BaseWindow
    {
        private var _tabData:Array;
        private var _tabBar:GTabarNew;
        private var _leftContinaer:Sprite;
        private var _list:GTileList;
        private var _txtSelectNum:GTextFiled;
        private var _pageSelecter:PageSelecter;
        private var _txtSelectAll:GTextFiled;
        private var _txtSelectInvert:GTextFiled;
        private var _pageTool:PageTool;
        private var _rightContinaer:Sprite;
        private var _txtTitle:GTextInput;
        private var _txtContent:MTextAreaBox;
        private var _txtCoin:GTextFiled;
        private var _btnSend:GButton;
        private var _selectArr:Array;
        private static var _instance:GuildMailPanel;

        public function GuildMailPanel(param1:ILayer = null)
        {
            this._pageTool = new PageTool([], 6);
            super(param1);
            if (_instance)
            {
                throw new Error("单例模式");
            }
            this.title = Language.getString(50001);
            this.setSize(635, 406);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._tabData = Language.getArray(71191);
            this._selectArr = [];
            this._tabBar = UIFactory.gTabBarNew(4, 28, this._tabData, 631, 376, 85, 26, this, null);
            this._leftContinaer = UIFactory.sprite(8, 59, this);
            this._rightContinaer = UIFactory.sprite(321, 59, this);
            pushUIToDisposeVec(UIFactory.bg(0, 0, 309, 342, this._leftContinaer));
            pushUIToDisposeVec(UIFactory.bg(0, 0, 309, 342, this._rightContinaer));
            this._list = UIFactory.tileList(5, 10, 300, 300, this._leftContinaer);
            this._list.columnWidth = 300;
            this._list.rowHeight = 48;
            this._list.setStyle("cellRenderer", GuildMailMemberListCellRenderer);
            this._list.configEventListener(Event.CHANGE, this.onListSelectChange);
            this._txtSelectNum = UIFactory.textField("" + Language.getString(71192), 8, 312, 80, 22, this._leftContinaer, GlobalStyle.textFormatLv);
            this._pageSelecter = UIFactory.pageSelecter(85, 312, this._leftContinaer, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, new GTextFormat());
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.pageTextBoxSize = 36;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._txtSelectAll = UIFactory.textField("" + Language.getString(71193), 238, 312, 35, 22, this._leftContinaer);
            this._txtSelectAll.htmlText = HTMLUtil.addColor("<u><a href=\'event:1\'>" + Language.getString(71193) + "</a></u>", GlobalStyle.colorLv);
            this._txtSelectAll.configEventListener(TextEvent.LINK, this.onTextLink);
            this._txtSelectInvert = UIFactory.textField("" + Language.getString(71194), 268, 312, 35, 22, this._leftContinaer);
            this._txtSelectInvert.htmlText = HTMLUtil.addColor("<u><a href=\'event:2\'>" + Language.getString(71194) + "</a></u>", GlobalStyle.colorLv);
            this._txtSelectInvert.configEventListener(TextEvent.LINK, this.onTextLink);
            pushUIToDisposeVec(UIFactory.textField("" + Language.getString(71195), 3, 8, 54, 20, this._rightContinaer));
            this._txtTitle = UIFactory.gTextInput(54, 10, 251, 22, this._rightContinaer);
            this._txtTitle.defaultText = "" + Language.getString(71195);
            this._txtContent = this.createTextAreaBox(5, 35, 300, 274, this._rightContinaer);
            this._txtContent.ta.text = "";
            this._txtContent.ta.maxChars = 100;
            pushUIToDisposeVec(UIFactory.textField("" + Language.getString(71196), 4, 315, 55, 20, this._rightContinaer, GlobalStyle.textFormatHuang));
            this._txtCoin = UIFactory.textField("100", 56, 315, 40, 20, this._rightContinaer);
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.Jinbi_bind, 85, this._txtCoin.y, this._rightContinaer));
            this._btnSend = UIFactory.gButton("" + Language.getString(71197), 230, 315, 70, 22, this._rightContinaer);
            this._btnSend.configEventListener(MouseEvent.CLICK, this.onSendClick);
            NetDispatcher.addCmdListener(ServerCommand.GUILD_MEMBERS_UPDATE, this.onMembersUpdate);
            this.update();
            return;
        }// end function

        private function onSendClick(event:MouseEvent) : void
        {
            var _loc_5:SGuildMember = null;
            var _loc_7:int = 0;
            var _loc_2:* = Cache.instance.cd.registerCDData(CDDataType.backPackLock, "GuildMailCD") as CDData;
            if (_loc_2 && _loc_2.isCoolDown)
            {
                _loc_7 = Math.ceil(_loc_2.leftTime / 1000 / 60);
                MsgManager.showRollTipsMsg(Language.getStringByParam(71198, _loc_7));
                return;
            }
            if (this._selectArr.length <= 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71199));
                return;
            }
            if (StringHelper.isWhitespace(this._txtTitle.text))
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71200));
                return;
            }
            if (StringHelper.isWhitespace(this._txtContent.ta.text))
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71201));
                return;
            }
            var _loc_3:Object = {};
            var _loc_4:Array = [];
            var _loc_6:int = 0;
            while (_loc_6 < this._selectArr.length)
            {
                
                _loc_5 = this._selectArr[_loc_6] as SGuildMember;
                _loc_4.push(_loc_5.miniPlayer.entityId.id);
                _loc_6++;
            }
            _loc_3.toPlayerIds = _loc_4;
            _loc_3.title = this._txtTitle.text;
            _loc_3.content = this._txtContent.ta.text;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_mail_send, _loc_3));
            return;
        }// end function

        private function onListSelectChange(event:Event) : void
        {
            var _loc_3:SGuildMember = null;
            var _loc_4:int = 0;
            var _loc_2:* = this._list.itemToCellRenderer(this._list.getItemAt(this._list.selectedIndex)) as GuildMailMemberListCellRenderer;
            if (_loc_2)
            {
                _loc_2.isCheck = !_loc_2.isCheck;
                if (_loc_2.isCheck)
                {
                    this._selectArr.push(_loc_2.data);
                }
                else
                {
                    _loc_4 = 0;
                    while (_loc_4 < this._selectArr.length)
                    {
                        
                        _loc_3 = this._selectArr[_loc_4] as SGuildMember;
                        if (_loc_3.miniPlayer.entityId.id == _loc_2.data.miniPlayer.entityId.id)
                        {
                            this._selectArr.splice(_loc_4, 1);
                            break;
                        }
                        _loc_4++;
                    }
                }
                this.updateSeclectNum();
            }
            this._list.selectedIndex = -1;
            return;
        }// end function

        private function onTextLink(event:TextEvent) : void
        {
            var _loc_3:GuildMailMemberListCellRenderer = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:SGuildMember = null;
            var _loc_2:Boolean = false;
            if (event.text == "1")
            {
                _loc_5 = this.getGuildMembers();
                if (_loc_5.length == this._selectArr.length)
                {
                    return;
                }
                this._selectArr.splice(0);
                _loc_6 = 0;
                while (_loc_6 < _loc_5.length)
                {
                    
                    _loc_7 = _loc_5[_loc_6] as SGuildMember;
                    this._selectArr.push(_loc_7);
                    _loc_6++;
                }
                _loc_2 = true;
            }
            else if (event.text == "2")
            {
                this._selectArr.splice(0);
                _loc_2 = false;
            }
            var _loc_4:int = 0;
            while (_loc_4 < this._list.length)
            {
                
                _loc_3 = this._list.itemToCellRenderer(this._list.getItemAt(_loc_4)) as GuildMailMemberListCellRenderer;
                _loc_3.isCheck = _loc_2;
                _loc_4++;
            }
            this.updateSeclectNum();
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this._list.selectedIndex = -1;
            this.updateList();
            return;
        }// end function

        private function onMembersUpdate(param1:Object) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.GUILD_MEMBERS_UPDATE, this.onMembersUpdate);
            this.update();
            return;
        }// end function

        private function update() : void
        {
            if (Cache.instance.guild.selfGuildInfo.memberList.length <= 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_memberList_get));
                return;
            }
            this.updateList();
            var _loc_1:* = new TextEvent(TextEvent.LINK);
            _loc_1.text = "1";
            this.onTextLink(_loc_1);
            return;
        }// end function

        public function updateList() : void
        {
            var _loc_1:GuildMailMemberListCellRenderer = null;
            var _loc_2:int = 0;
            var _loc_3:SGuildMember = null;
            var _loc_4:int = 0;
            this._pageTool.array = this.getGuildMembers();
            this._pageSelecter.maxPage = this._pageTool.pagesCount;
            this._list.dataProvider = new DataProvider(this._pageTool.getByPageNo(this._pageSelecter.currentPage));
            this._list.drawNow();
            _loc_2 = 0;
            while (_loc_2 < this._list.length)
            {
                
                _loc_1 = this._list.itemToCellRenderer(this._list.getItemAt(_loc_2)) as GuildMailMemberListCellRenderer;
                _loc_4 = 0;
                while (_loc_4 < this._selectArr.length)
                {
                    
                    _loc_3 = this._selectArr[_loc_4] as SGuildMember;
                    if (_loc_3.miniPlayer.entityId.id == _loc_1.data.miniPlayer.entityId.id)
                    {
                        _loc_1.isCheck = true;
                        break;
                    }
                    _loc_4++;
                }
                _loc_1.isCheck = false;
                _loc_2++;
            }
            this.updateSeclectNum();
            return;
        }// end function

        private function updateSeclectNum() : void
        {
            this._txtSelectNum.htmlText = HTMLUtil.addColor(Language.getStringByParam(71202, this._selectArr.length), GlobalStyle.colorLv);
            return;
        }// end function

        private function createTextAreaBox(param1:int, param2:int, param3:int = 117, param4:int = 23, param5:DisplayObjectContainer = null) : MTextAreaBox
        {
            var _loc_6:* = UICompomentPool.getUICompoment(MTextAreaBox);
            _loc_6.createDisposedChildren();
            _loc_6.bg.width = param3;
            _loc_6.ta.width = param3;
            _loc_6.bg.height = param4;
            _loc_6.ta.height = param4;
            _loc_6.ta.editable = true;
            _loc_6.x = param1;
            _loc_6.y = param2;
            if (param5)
            {
                param5.addChild(_loc_6);
            }
            return _loc_6;
        }// end function

        private function getGuildMembers() : Array
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo.memberList.concat();
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                if (_loc_1[_loc_2].miniPlayer.entityId.id == Cache.instance.role.entityInfo.entityId.id)
                {
                    _loc_1.splice(_loc_2, 1);
                }
                _loc_2++;
            }
            return _loc_1;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            NetDispatcher.removeCmdListener(ServerCommand.GUILD_MEMBERS_UPDATE, this.onMembersUpdate);
            this._tabData = null;
            this._selectArr = null;
            this._tabBar.dispose(param1);
            this._list.dispose(param1);
            this._txtSelectNum.dispose(param1);
            this._pageSelecter.dispose(param1);
            this._txtSelectAll.dispose(param1);
            this._txtSelectInvert.dispose(param1);
            this._txtTitle.dispose(param1);
            this._txtContent.dispose(param1);
            this._txtCoin.dispose(param1);
            this._btnSend.dispose(param1);
            if (this._leftContinaer.parent)
            {
                this._leftContinaer.parent.removeChild(this._rightContinaer);
            }
            if (this._rightContinaer.parent)
            {
                this._rightContinaer.parent.removeChild(this._rightContinaer);
            }
            this._tabBar = null;
            this._list = null;
            this._txtSelectNum = null;
            this._pageSelecter = null;
            this._txtSelectAll = null;
            this._txtSelectInvert = null;
            this._txtTitle = null;
            this._txtContent = null;
            this._txtCoin = null;
            this._btnSend = null;
            this._leftContinaer = null;
            this._rightContinaer = null;
            return;
        }// end function

        public static function get instance() : GuildMailPanel
        {
            if (_instance == null)
            {
                _instance = new GuildMailPanel;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
